package com.jz.digital.base;

import android.os.Handler;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.concurrent.Future;

/**
 * Created by lyw on 2018/3/29 0029.
 * MVP 中
 * 所有P 的顶级父类
 * V 表示绑定的View 类型,必须实现IBaseView接口
 */
public abstract class BasePresenter<V extends IBaseView> {
    protected WeakReference<V> mWeak;
    private Handler mHandler = null;
    private ArrayList<Future> futures;

    /**
     * 绑定的View接口
     *
     * @param v
     */
    public void attachView(V v) {
        mWeak = new WeakReference<>(v);
        init();
    }

    public void init() {
    }

    /**
     * 解绑当前View
     *
     * @return
     */
    public void detachView() {
        if (mWeak != null) {
            mWeak.clear();
        }
    }

    /**
     * 当前View是否存在，或者没被销毁
     *
     * @return
     */
    protected boolean isAttachView() {
        return mWeak != null && mWeak.get() != null;
    }

    protected V getView() {
        if (mWeak != null) {
            return mWeak.get();
        }
        return null;
    }
}
